<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set value="${pageContext.request.contextPath}" var="path" scope="page"/>
<!DOCTYPE html>
<html>

<head>
    <title>用户列表</title>
    <link rel="stylesheet" href="../../../backstage/css/x-admin.css" media="all">
    <script src="../../../backstage/js/jquery.min.js"></script>
    <script src="../../../backstage/lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../backstage/js/x-layui.js" charset="utf-8"></script>
</head>

<body>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a><cite>首页</cite></a>
        <a><cite>用户管理</cite></a>
        <a><cite>用户列表</cite></a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i>
    </a>
</div>
<div class="x-body">
    <form class="layui-form x-center" action="" style="width:800px">
        <div class="layui-form-pane" style="margin-top: 15px;">
            <div class="layui-form-item">
                <label class="layui-form-label">日期范围</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="beginCreateDate"placeholder="开始日期" id="LAY_demorange_s">
                </div>
                <div class="layui-input-inline">
                    <input class="layui-input" name="endCreateDate"placeholder="截止日期" id="LAY_demorange_e">
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="username"name="username" placeholder="请输入用户名" autocomplete="off"
                           class="layui-input" maxlength="10">
                </div>
                <div class="layui-input-inline">
                    <input type="text" id="id" name="id" placeholder="请输入ID" autocomplete="off" class="layui-input"
                           maxlength="10" onkeyup="value=this.value.replace(/\D+/g,'')">
                </div>
                <div class="layui-input-inline" id="search"style="width:80px">
                    <input type="button"  class="layui-btn"  lay-submit="" lay-filter="sreach" value="搜索">
                    </input>
                </div>
            </div>
        </div>
    </form>
    <div id="block">
        <%--<button class="layui-btn layui-btn-danger" onclick="member_edit('修改状态','member-edit.jsp','4','600','500')">--%>
            <%--<i class="layui-icon">&#xe642;</i>修改用户状态--%>
        <%--</button>--%>
        <button class="layui-btn" onclick="member_add('添加用户','${path}/admin/userAdd','600','500')"> <i class="layui-icon">&#xe608;</i>添加
        </button>
    </div>
    <table class="layui-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>性别</th>
                <th>创建时间</th>
                <th>角色</th>
                <th>更新时间</th>
                <th>状态</th>
                <th>重置密码</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="user_tbody">
            <tr>
                <%--<td>1</td>--%>
                <%--<td>--%>
                    <%--<div style="cursor:pointer" >--%>
                        <%--风羽--%>
                    <%--</div>--%>
                <%--</td>--%>
                <%--<td>男</td>--%>
                <%--<td>2018-01-01 11:11:42</td>--%>
                <%--<td>用户</td>--%>
                <%--<td>2018-01-01 11:11:42</td>--%>
                <%--<td>有效</td>--%>
                <%--<td class="td-manage">--%>
                    <%--<a title="编辑" href="javascript:;" onclick="member_edit('编辑','${path}/admin/userEdit','4','600','500')" class="ml-5" style="text-decoration:none">--%>
                        <%--<i class="layui-icon">&#xe642;</i>--%>
                    <%--</a>--%>
                <%--</td>--%>
            </tr>
        </tbody>
    </table>
    <div id="page" style="margin-left: 650px;"></div>
</div>

<script>
layui.use(['laydate', 'element', 'laypage', 'layer'], function() {
    $ = layui.jquery; //jquery
    laydate = layui.laydate; //日期插件
    lement = layui.element(); //面包导航
    laypage = layui.laypage; //分页
    layer = layui.layer; //弹出层

    laypage({
        cont: 'page',
        pages: 100,
        first: 1,
        last: 100,
        prev: '<em><</em>',
        next: '<em>></em>'
    });

    var start = {
        min: '1099-06-16 23:59:59',
        max: '2099-06-16 23:59:59',
        istoday: false,
        choose: function(datas) {
            end.min = datas;
            end.start = datas;
        }
    };
    var end = {
        min: '1099-06-16 23:59:59',
        max: '2099-06-16 23:59:59',
        istoday: false,
        choose: function(datas) {
            start.max = datas;
        }
    };
    document.getElementById('LAY_demorange_s').onclick = function() {
        start.elem = this;
        laydate(start);
    };
    document.getElementById('LAY_demorange_e').onclick = function() {
        end.elem = this;
        laydate(end);
    };
});


// 添加
function member_add(title, url, w, h) {
    x_admin_show(title, url, w, h);
}
// 重置密码
function member_show(user) {
   // window.localStorage.setItem('msg',$(user).data("id"));
   // x_admin_show("编辑", "${path}/admin/pwdReset", 510,400);

    layer.confirm('确认要重置吗？', function(index) {
        var id = $(user).data("id");
        $.ajax({
            type: 'POST',
            url: '/userManager/selectById',
            dataType: "json",
            data: {uid: id},
            error: function () {
                console.log("失败");
            },
            success: function (result) {

                    $.ajax({
                        type: 'POST',
                        url: '/user/changePwd',
                        dataType: "json",
                        data: {
                            username: result.username,
                            oldPwd: result.pwd,
                            newPwd: "123456"
                        },
                        error: function () {
                            alert("失败");
                        },
                        success: function (data) {
                            console.log(data);
                            if (data.msg) {

                                layer.msg('重置成功!', { icon: 1, time: 1000 });
                            }
                        }
                    });
            }
        });
    });
}
// 编辑
function member_edit(user) {
    //console.log($(user).data("id"));
    //将localStorage传递到哪个页面
    //设置localStorage
    window.localStorage.setItem('msg',$(user).data("id"));
    x_admin_show("编辑", "${path}/admin/userEdit", 510,400);
}
$("#search").click(function() {
    var uid = $.trim($("#id").val());
    var username = $.trim($("#username").val());
    var beginCreateDate = $.trim($("#LAY_demorange_s").val());
    var endCreateDate = $.trim($("#LAY_demorange_e").val());

    $.ajax({
        type: 'POST',
        url: '/userManager/selectElasticly',
        dataType: "json",
        data: {
            uid: uid,
            username: username,
            sex: null, role: null,
            state: null,
            beginCreateDate: beginCreateDate,
            endCreateDate: endCreateDate,
            beginUpdateDate: null,
            endUpdateDate: null
        },
        error: function () {
            alert("查询失败");
        },
        success: function (data) {
            //alert(JSON.stringify(data));
            //后台方法成功被调用查询到了数据，alert出来了
            console.info(JSON.stringify(data));
            var str = '';
            for (i = 0; i < data.length; i++) {
                var user = data[i];
                str += '<tr>';
                str += '  <td>' + user.id + '</td>';
                str += '  <td>' + user.username + '</td>';
                str += '  <td>' + user.sex + '</td>';
                str += '  <td>' + new Date(user.createDate).toLocaleDateString().replace(/\//g, "-") + " " + new Date(user.createDate).toTimeString().substr(0, 8) + '</td>';
                str += '  <td>' + user.role + '</td>';
                str += '  <td>' + new Date(user.updateDate).toLocaleDateString().replace(/\//g, "-") + " " + new Date(user.updateDate).toTimeString().substr(0, 8) + '</td>';
                str += '  <td>' + user.state + '</td>';
                str += '  <td class="td-manage"> <button id="reset" class="layui-btn layui-btn-danger" onclick="member_show(this)" data-id=' + user.id + '>重置</button></td>';
                str += '  <td class="td-manage"> <a title="编辑" class="ml-5" style="text-decoration:none" href="javascript:void(0)" onclick="member_edit(this)" data-id=' + user.id + ' >' + ' <i class="layui-icon">' + "&#xe642;" + '</i> </a> </td>';
                str += '</tr>';
                $('#user_tbody').html(str);
            }
        }
    });
});


</script>
</body>

</html>